import requests
import json
import datetime

day = datetime.datetime.now().strftime("%Y-%m-%d")



def information(url):
    get_weather(api_key='531b7f1951343b16990631805421a41e', city="Shanghai")

    param = {
        "msgtype": "markdown",
        "markdown": {
            "content": f"## 小果提醒\n\n> **日期**：{day} \n\n>**天气**：<font>{translate_weather(weather_data)}</font>\n\n> 元气满满的一天开始了，小果提醒大家准时打卡的同时也要记得点餐呦"
        }
    }

    r = requests.post(url=url,json=param)  # url中的xxx是你的机器人webhook对应片段，不要轻易泄露出去否则任何人都可以控制你的机器人哦
    print(r.text)




def get_weather(api_key, city):
    base_url = "http://api.openweathermap.org/data/2.5/weather"
    params = {
        'q': city,
        'appid': api_key,
        'units': 'metric'
    }
    response = requests.get(base_url, params=params)
    weather_data = response.json()
    return weather_data

# api_key =   # 替换为你的API密钥
# city = 'Changsha'  # 替换为你想查询的城市
weather_data = get_weather(api_key='531b7f1951343b16990631805421a41e', city="Shanghai")
print(json.dumps(weather_data, indent=4))


# 获取天气
def translate_weather(weather_data):
    weather_dict = {
        "Clear": "晴朗",
        "Clouds": "多云",
        "Rain": "雨",
        "Snow": "雪",
        # 添加更多天气翻译
    }
    weather_description = weather_data['weather'][0]['main']
    weather_description_cn = weather_dict.get(weather_description, "未知")  # 根据返回的value为键,在获取对应的翻译
    return weather_description_cn


# weather_description_cn = translate_weather(weather_data)
# print(f"天气：{translate_weather(weather_data)}")


information(url='https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=ed6642d8-42e7-465f-9f31-9936e6673bec')